package com.neusoft.bizcore.mom.messaging.impl;

import javax.jms.ConnectionFactory;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.mom.bean.MessageBean;
import com.neusoft.bizcore.mom.messaging.ProducerService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component("activemqProducerService")
@ConditionalOnClass({ConnectionFactory.class, ActiveMQConnectionFactory.class })
@ConditionalOnMissingBean(name = "kafkaProducerService")
public class ActivemqProducerServiceImpl implements ProducerService {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Override
    public void send(final String topic, final MessageBean message) {
        this.jmsMessagingTemplate.convertAndSend(topic, message);
        if (ActivemqProducerServiceImpl.log.isDebugEnabled()) {
            ActivemqProducerServiceImpl.log.debug("send to activemq topic {} a message {}", topic,
                    message.getMessage());
        } else if (ActivemqProducerServiceImpl.log.isInfoEnabled()) {
            ActivemqProducerServiceImpl.log.info("send to activemq topic {} a message", topic);
        }
    }
}
